App({
  globalData: {
    score: 0,
    title: '新手玩家',
    titles: [
      { minScore: 0, title: "新手小白" },
      { minScore: 5, title: "入门玩家" },
      { minScore: 10, title: "初级高手" },
      { minScore: 30, title: "中级玩家" },
      { minScore: 80, title: "中级高手" },
      { minScore: 200, title: "高级玩家" },
      { minScore: 500, title: "高级高手" },
      { minScore: 1000, title: "大师级玩家" },
      { minScore: 5000, title: "宗师级玩家" },
      { minScore: 10000, title: "传奇玩家" }
    ]
  },
  onLaunch: function () {
    // 尝试从本地存储中读取积分
    const storedScore = wx.getStorageSync('score');
    if (storedScore) {
      this.globalData.score = storedScore;
      this.globalData.title = this.getTitleByScore(storedScore);
    }
  },
  getTitleByScore: function (score) {
    for (let i = this.globalData.titles.length - 1; i >= 0; i--) {
      if (score >= this.globalData.titles[i].minScore) {
        return this.globalData.titles[i].title;
      }
    }
    return this.globalData.titles[0].title;
  },
  // 检查称号是否升级的方法
  checkTitleUpgrade: function () {
    const score = this.globalData.score;
    let newTitle = this.globalData.title;
    // 遍历 titles 数组，根据当前积分找到对应的称号
    for (let i = this.globalData.titles.length - 1; i >= 0; i--) {
      if (score >= this.globalData.titles[i].minScore) {
        newTitle = this.globalData.titles[i].title;
        break;
      }
    }
    // 如果新称号与当前称号不同，更新称号并返回 true 表示升级
    if (newTitle !== this.globalData.title) {
      this.globalData.title = newTitle;
      return true; 
    }
    return false; 
  },
  // 封装播放提示音的方法
  playSound: function (soundType) {
    const soundMap = {
      enniu: 'enniu.mp3',
      next: 'next.mp3',
      upgrade: 'upgrade.mp3'
    };
    const audioUrl = `/pages/assets/audio/${soundMap[soundType]}`;
    const innerAudioContext = wx.createInnerAudioContext();
    innerAudioContext.src = audioUrl;
    innerAudioContext.play();
    innerAudioContext.onError((res) => {
      console.error('音频播放出错:', res.errMsg);
    });
  }
})